/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package evabot.xml;

//package alicebot.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Classe responsÃ¡vel pela ediÃ§Ã£o do AIML.
 *
 * @author Alexandre Sales, Danielle Chaves, Maria Isabel Farias
 *
 */
public class AIMLEditor {

	Document doc;
	Element rootNode;

	/**
	 * Construtor da classe.
	 *
	 * @param doc
	 * 		O documento DOM do XML.
	 */
	public AIMLEditor(Document doc) {
		this.doc = doc;
		this.rootNode = doc.getDocumentElement();
	}



	/**
	 * ResponsÃ¡vel pela inserÃ§Ã£o de novas categorias no AIML.
	 *
	 * @param question
	 * 			A pergunta a ser inserida.
	 * @param answer
	 * 			A resposta fornecida pelo usuÃ¡rio.
	 */
	public void insertCategory(String question, String answer){
        Node aiml = doc.getFirstChild();

        Node category = doc.createElement("category");

        String patternContent = "";

        String auxQuestion = question.substring(11,question.indexOf("eh"));

        // tratando a string de entrada para o padrÃ£o da tag AIML <pattern>
        Pattern aux = Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]");
        Matcher matcher = aux.matcher(auxQuestion.toUpperCase());
        while (matcher.find()) {
        // imprimi o retorno tratado
        	patternContent += matcher.group();
        }
        Node pattern = doc.createElement("pattern");
        pattern.setTextContent("*"+patternContent+"*");

        auxQuestion = question.substring(question.indexOf("eh"),question.length());

        Node template = doc.createElement("template");
        template.setTextContent("Eu sei que" + patternContent.toLowerCase() + auxQuestion);

        aiml.appendChild(category);
        category.appendChild(pattern);
        category.appendChild(template);

        Transformer transformer = null;

        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException e1) {
        	e1.printStackTrace();
        } catch (TransformerFactoryConfigurationError e1) {
        	e1.printStackTrace();
        }

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //initialize StreamResult with File object to save to file
        StreamResult result = null;

        try {
            result = new StreamResult(new FileWriter(new File("Alice.xml")));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        DOMSource source = new DOMSource(doc);

        try {
            transformer.transform(source, result);
        } catch (TransformerException e) { e.printStackTrace(); }
       }

}